package net.sxnic.ugr.role;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import net.sxinfo.core.entity.AbstractEntity;
import net.sxnic.ugr.menu.Menu;
import net.sxnic.ugr.permission.Permission;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.IndexColumn;

/**
 * 角色实体类
 * 
 * @author 孙宇飞
 * 
 */
@Entity
@Table(name = "ugr_role")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Role extends AbstractEntity {

	/**
	 * serial
	 */
	private static final long serialVersionUID = -8021163772081891564L;

	/**
	 * 角色：系统管理员
	 */
	public static final String SYSTEM_ADMIN = "admin";

	/**
	 * 角色代号，使用英文
	 */
	@IndexColumn(name = "role_index")
	@Column(unique = true, nullable = false, name = "role_code")
	private String role;

	/**
	 * 角色名称
	 */
	@Column(nullable = false, name = "role_name")
	private String name;

	/**
	 * 所属子系统code ,如果未空表示公用，如果有值则表示子系统专用
	 */
	private String siteCode;

	/**
	 * 权限
	 */
	@ManyToMany(fetch = FetchType.LAZY)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private Set<Permission> permissions;

	/**
	 * 菜单
	 */
	@ManyToMany(fetch = FetchType.LAZY)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private Set<Menu> menus;

	/**
	 * 默认无参构造器
	 */
	public Role() {
		super();
		permissions = new HashSet<Permission>();
		menus = new HashSet<Menu>();
	}

	/**
	 * 构造器
	 * 
	 * @param name
	 *            角色名
	 */
	public Role(String name) {
		this.name = name;
	}

	/**
	 * 添加权限到角色
	 * 
	 * @param p
	 *            要添加的权限
	 */
	public void addPermission(Permission p) {

		permissions.add(p);

		// p.getRoles().add(this);

	}

	/**
	 * 从角色中删除权限
	 * 
	 * @param p
	 *            要删除的权限
	 */
	public void removePermission(Permission p) {
		permissions.remove(p);
	}

	public void addMenu(Menu menu) {
		menus.add(menu);
	}

	public void removeMenu(Menu menu) {
		menus.remove(menu);
	}

	/**
	 * @return Returns the role.
	 */
	public String getRole() {
		return role;
	}

	/**
	 * @param role
	 *            The role to set.
	 */
	public void setRole(String role) {
		this.role = role;
	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object o) {
		if (!(o instanceof Role)) {
			return false;
		}

		final Role r = (Role) o;

		return new EqualsBuilder().append(role, r.getRole())
				.append(name, r.getName()).isEquals();
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(role).append(name).toHashCode();
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	public Set<Permission> getPermissions() {
		return permissions;
	}

	public void setPermissions(Set<Permission> permissions) {
		this.permissions = permissions;
	}

	public Set<Menu> getMenus() {
		return menus;
	}

	// 获取拥有的菜单Id列表
	public String[] getMenuIds() {

		String[] mids;

		if (menus != null && menus.size() > 0) {
			mids = new String[menus.size()];
			int i = 0;
			for (Menu m : menus) {
				mids[i] = m.getId();
				i++;
			}
		} else {
			mids = new String[0];
		}

		return mids;
	}

	// 获取拥有的菜单Id列表
	public String[] getPermIds() {

		String[] pids;

		if (permissions != null && permissions.size() > 0) {
			pids = new String[permissions.size()];
			int i = 0;
			for (Permission p : permissions) {
				pids[i] = p.getId();
				i++;
			}
		} else {
			pids = new String[0];
		}

		return pids;
	}

	public void setMenus(Set<Menu> menus) {
		this.menus = menus;
	}

	public String getSiteCode() {
		return siteCode;
	}

	public void setSiteCode(String siteCode) {
		this.siteCode = siteCode;
	}
}
